LanguageExt.Core

LanguageExt.Core Monads Monadic conditionals

guard, when, and unless allow for conditional operations and short-cutting in monadic expressions.

Guards

Guards are used to stop the monadic expression continuing if a flag is true (for guard) or false (for guardnot).

They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left in Either for example). An alternative value is provided when the guard triggers:

from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;

Supported monads are:

Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff

When and Unless

when and unless are similar to guards, but instead of providing the alternative value, you provide an alternative monad to run. This monad could be in a failed state, or it could run a successful side effect (an Eff calling Console<RT>.writeLine() for example).

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

Contents

class Prelude Source #

Methods

method K<F, Unit> guard <F> (bool flag) Source #

where F : MonoidK<F>, Applicative<F>

Guard against continuing an applicative expression

Parameters

param flag

Flag for continuing

returns

Applicative that yields () if flag is true; otherwise it yields Applicative.Empty - shortcutting the expression

method IO<Unit> guardIO (bool flag) Source #

Guard against continuing an applicative expression

Parameters

param flag

Flag for continuing

returns

Applicative that yields () if flag is true; otherwise it yields Applicative.Empty - shortcutting the expression

method Guard<E, Unit> guard <E> (bool flag, Func<E> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<E, Unit> guard <E> (bool flag, E False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guard (bool flag, Func<Error> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guard (bool flag, Error False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

class Prelude Source #

Methods

method Guard<E, Unit> guardnot <E> (bool flag, Func<E> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<E, Unit> guardnot <E> (bool flag, E True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guardnot (bool flag, Func<Error> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error, Unit> guardnot (bool flag, Error True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

class Prelude Source #

Methods

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<F, A> iff <F, A> (bool Pred, K<F, A> Then, K<F, A> Else) Source #

If this then that for higher-kinds

Parameters

type F

Trait type

type A

Bound return value

param Pred

Boolean flag to be computed

param Then

Then branch if the flag computes to true

param Else

Else branch if the flag computes to false

returns

Returns either the then or else branches depending on the computed flag

class Prelude Source #

Methods

method K<F, Unit> unless <F> (bool flag, K<F, Unit> alternative) Source #

where F : Applicative<F>

Conditional execution of Applicative expressions

Run the alternative when the flag is false, return pure () when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method K<M, Unit> unless <M> (K<M, bool> Pred, K<M, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to false, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> unless <M> (K<M, bool> Pred, K<IO, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to false, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> unless <M> (K<M, bool> Pred, Pure<Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to false, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

class Prelude Source #

Methods

method K<F, Unit> when <F> (bool flag, K<F, Unit> alternative) Source #

where F : Applicative<F>

Conditional execution of Applicative expressions

Run the alternative when the flag is true, return pure () when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method K<M, Unit> when <M> (K<M, bool> Pred, K<M, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to true, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> when <M> (K<M, bool> Pred, K<IO, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to true, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> when <M> (K<M, bool> Pred, Pure<Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to true, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad